﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//unpkg.com/layui@2.10.3/dist/css/layui.css" rel="stylesheet">
</head>

<body>
    <form class="layui-form" action="" id="baseStoreForm">
        <div class="layui-input-inline">
            <label class="layui-input-inline" style="width:auto">单号</label>
            <div class="layui-input-inline">
                <input type="text" name="stae" class="layui-input">
            </div>
            <label class="layui-input-inline" style="width:auto">客户名称</label>
            <div class="layui-input-inline">
                <input type="text" name="urcName" class="layui-input">
            </div>
            <button class="layui-btn" lay-submit lay-filter="search">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            @* <b><a style="color:red" href="http://localhost:8075/webroot/decision/view/report?viewlet=%25E4%25BB%2593%25E5%25BA%2593%25E5%2585%25A5%25E5%25BA%2593%25E6%2598%258E%25E7%25BB%2586%25E8%25A1%25A8.cpt" target="_blank">采购入库明细表(帆软)</a></b> *@
            @* <b><a style="color:red" href="http://localhost:8075/webroot/decision/view/report?viewlet=%25E4%25BB%2593%25E5%25BA%2593%25E5%2587%25BA%25E5%2585%25A5%25E6%2598%258E%25E7%25BB%2586%25E8%25A1%25A8.cpt" target="_blank">仓库入库明细表(帆软)</a></b> *@
        </div>
    </form>
    <!-- 表单内容保持不变... -->
    <table class="layui-hide" id="ID-table-demo-filter"></table>
    <script src="//unpkg.com/layui@2.10.3/dist/layui.js"></script>

    <!-- 操作列按钮模板 -->
    <script type="text/html" id="tableToolbar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> 
         @* <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="audit">状态</a> *@
    </script>

    <script>
        layui.use(['form', 'table', 'laydate', 'layer'], function () {
            var $ = layui.$;
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;
            var layer = layui.layer;

            // 初始化日期选择器
            laydate.render({ elem: '#begindate', type: 'date' });
            laydate.render({ elem: '#enddate', type: 'date' });

            table.render({
                elem: '#ID-table-demo-filter',
                toolbar: 'default',
                height: 315,
                cols: [[
                    { field: 'urcid', title: '序号', width: 80, fixed: 'left' },
                    { field: 'urcStae', title: '销售单号', width: 120 },
                    { field: 'urcic', title: '销售订单主题', width: 120 },
                    { field: 'urcName', title: '客户名称', width: 100 },
                    { field: 'urcTime', title: '销售日期', width: 100 },
                 {
            field: 'billingsale',
            title: '出库状态',
            width: 100,
            templet: function (d) {
                return d.billingsale? '未完成' : '已完成';
            }
        },
                    { field: 'urcamount', title: '总金额', width: 100 },
                    { field: 'urceamount', title: '已收票金额', width: 100 },
                    {
                        field: 'operation',
                        title: '操作',
                        width: 150,
                        fixed: 'right',
                        toolbar: '#tableToolbar'
                    }
                ]],
                done: function () {
                    // 记录筛选状态
                    var that = this;
                    that.elem.next().on('mousedown', 'input[lay-filter="LAY_TABLE_TOOL_COLS"]+', function () {
                        var input = $(this).prev()[0];
                        layui.data('table-filter-test', {
                            key: input.name,
                            value: input.checked
                        });
                    });
                },
                page: true, // 开启分页
                limits: [2, 5, 10, 15], // 每页数据量选项
                limit: 2 // 默认每页数据量
            });

            // 监听工具条事件
            table.on('tool(ID-table-demo-filter)', function (obj) {
                if (obj.event === 'edit') {
                    var data = obj.data;
                    // 将整行数据编码为JSON并传递
                    var encodedData = encodeURIComponent(JSON.stringify(data));
                 // @ViewBag.BaseUrl/T7/Procurements/PuturchaseorderVoew?data=${encodedData}
                    window.location.href = `/Procurements/PuturchaseOrdp?data=${encodedData}`;
                } else if (obj.event === 'audit') {
                    var data = obj.data;
                    // 使用 toString() 方法转换，也可用 String() 函数
                    layer.confirm('确定要审核该单据吗？', {
                        btn: ['确定', '取消']
                    }, function () {
                        // 模拟审核请求
                        $.ajax({
                            //url: 'http://localhost:5090/api/Procurements/UpdateAlpe',
                            url: "@ViewBag.BaseUrl/T8/Procurements/UpdateAlpe",

                            data: JSON.stringify({
                                purchaseOrderId: data.purchaseOrderId,
                                businessStatus: data.businessStatus.toString(),
                                auditDate: data.auditDate.toString()
                            }),
                            type: 'post',
                            dataType: 'json',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            success: function (res) {
                                if (res.code === 207) {
                                    layer.msg('审核成功', { icon: 1 });
                                    loadRoleData(); // 刷新表格数据
                                } else {
                                    layer.msg('审核失败: ' + (res.message || '未知错误'), { icon: 2 });
                                }
                            },
                            error: function (xhr, status, error) {
                                layer.msg('请求失败: ' + error, { icon: 2 });
                            }
                        });
                    }, function () {
                        layer.msg('已取消审核', { icon: 2 });
                    });
                }
            });

            function loadRoleData() {
                layer.load(2);
                var params = {
                    stae: $('[name="stae"]').val(),
                    urcName: $('[name="urcName"]').val(),
                    pageIndex: $('.layui-laypage-curr em').text() || 1, // 获取当前页码，若无则为1
                    pageSize: $('.layui-laypage-limits select').val() // 获取当前每页数据量
                };

                $.ajax({
                    //url: 'http://localhost:5234/api/Customermanage/Geturchase',
                    url: "@ViewBag.BaseUrl/T7/Customermanage/Geturchase",

                    data: params,
                    type: 'get',
                    dataType: 'json',
                    success: function (res) {
                        layer.closeAll('loading');
                        if (res.code === 0) {
                            table.reload('ID-table-demo-filter', {
                                data: res.data.list,
                                page: {
                                    curr: parseInt(params.pageIndex), // 设置当前页码
                                    count: res.data.totalCount // 设置总数据量，用于分页计算
                                }
                            });
                        } else {
                            layer.msg('获取数据失败: ' + (res.message || '未知错误'), { icon: 2 });
                        }
                    },
                    error: function (xhr, status, error) {
                        layer.closeAll('loading');
                        layer.msg('请求失败: ' + error, { icon: 2 });
                    }
                });
            }

            // 监听查询按钮提交事件
            form.on('submit(search)', function (data) {
                loadRoleData();
                return false;
            });

            // 初始加载数据
            loadRoleData();
        });
    </script>
</body>

</html>